package com.summer.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.summer.reggie.common.BaseContext;
import com.summer.reggie.common.R;
import com.summer.reggie.entity.ShoppingCart;
import com.summer.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 作者:夏天
 * qq:1205232048
 */
@RestController
@Slf4j
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        log.info("购物车数据: {}",shoppingCart);
        //得到用户Id
        Long userId = BaseContext.getCurrentId();
        //将购物车与用户Id进行绑定
        shoppingCart.setUserId(userId);
        //从加入购物车中取出Id
        Long dishId = shoppingCart.getDishId();
        //构造条件封装器
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        //用户Id等值查询条件
        queryWrapper.eq(ShoppingCart::getUserId, userId);
        //如果菜品Id不为空
        if(dishId != null){
            //就说明加入购物车的是菜品
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
        }else {
            //说明加入购物车的是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        //查询满足上面条件的菜品或套餐
        ShoppingCart dishOrSetmeal = shoppingCartService.getOne(queryWrapper);
        //如果已经存在 那么就在原来的数量+1
        if(dishOrSetmeal != null){
            Integer number = dishOrSetmeal.getNumber();
            dishOrSetmeal.setNumber(number + 1);
            //更新一下数量
            shoppingCartService.updateById(dishOrSetmeal);
        }else {//如果不存在 则添加到购物车 数量默认就是一
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            dishOrSetmeal = shoppingCart;
        }

        return R.success(dishOrSetmeal);
    }

    /**
     * 查看购物车
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        log.info("查看购物车...");
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
        return R.success(list);
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean(){
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        shoppingCartService.remove(queryWrapper);
        return R.success("清空购物车成功");
    }

}
